package com.example.administrator.hqq.View.alertview;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.ReportListAdapter;
import com.example.administrator.hqq.adapter.discover.ReleasePostAddGroupListAdapter;
import com.example.administrator.hqq.bean.post.MyGroupListBean;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.bean.Image;


public class AlertViewFactory {
    private static volatile AlertViewFactory defaultInstance;
    private int index=-1;
    public static AlertViewFactory getInstance() {
        if (defaultInstance == null) {
            synchronized (AlertViewFactory.class) {
                if (defaultInstance == null) {
                    defaultInstance = new AlertViewFactory();
                }
            }
        }
        return defaultInstance;
    }

    private AlertViewFactory() {
    }


    /**
     * 图片类型选择
     */
    public AlertView getPhotoTypeAlertView(final Context context, OnItemClickListener onItemClickListener) {
        AlertView mAlertView;
        mAlertView = null;
        mAlertView = new AlertView(null, null, context.getResources().getString(R.string.cancel),
                null, context.getResources().getStringArray(R.array.photo_type), context,
                AlertView.Style.ActionSheet, onItemClickListener).setCancelable(true);
        return mAlertView;
    }

    /**
     * 图片类型选择
     */
    public AlertView getPhotoAlertView(final Context context, OnItemClickListener onItemClickListener) {
        AlertView mAlertView;
        mAlertView = null;
        mAlertView = new AlertView(null, null, context.getResources().getString(R.string.cancel),
                null, context.getResources().getStringArray(R.array.photo), context,
                AlertView.Style.ActionSheet, onItemClickListener).setCancelable(true);
        return mAlertView;
    }

    /**
     * 带布局显示的图片类型选择
     */
    public AlertView getShowPicPhotoTypeAlertView(int layoutId, final Activity context, OnItemClickListener onItemClickListener) {
        AlertView mAlertView;
        mAlertView = null;
        mAlertView = new AlertView(layoutId, null, null, context.getResources().getString(R.string.cancel),
                null, context.getResources().getStringArray(R.array.photo_type), context,
                AlertView.Style.ActionSheet, onItemClickListener).setCancelable(true);
        return mAlertView;
    }

    /**
     * @param context 上下文
     * @param list 举报数据
     * @param reportOnClickListener 设置点击事件回调监听
     * @return  AlerDialog对象
     */
    public AlertDialog getShowReportAlertView(Context context, List<String> list, final ReportOnClickListener reportOnClickListener){
        index=-1;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        View inflate = LayoutInflater.from(context).inflate(R.layout.report_popup_view, null);
        final ListView reportLV = inflate.findViewById(R.id.report_lv);
        Button submit = inflate.findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (reportOnClickListener!=null)
                {
                    if (index<0)
                    {
                        ToastUtils.showShort("请选择要举报的原因");
                    }else{
                        reportOnClickListener.submit(index);
                    }

                }
            }
        });
        AutoRelativeLayout cancel = inflate.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (alertDialog !=null&& alertDialog.isShowing())
                alertDialog.cancel();

            }
        });
        final ReportListAdapter reportListAdapter = new ReportListAdapter(list, context);
        reportLV.setAdapter(reportListAdapter);
        reportListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                index=position;
                reportListAdapter.setPosition(position);
                reportListAdapter.notifyDataSetChanged();
            }
        });
        alertDialog.getWindow().setContentView(inflate);
        Window window = alertDialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width= dp2px(context,283);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(attributes);
        return alertDialog;
    }
    public AlertDialog showLeaveGroupReport(Context context,final DeleteFriendReportOnclicke deleteFriendReportOnclicke){
        index=0;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog alertDialog = builder.create();
        View inflate = LayoutInflater.from(context).inflate(R.layout.leave_group_report_dialog_layout, null);
        Button yes = inflate.findViewById(R.id.yes);
        Button no = inflate.findViewById(R.id.no);
        AutoLinearLayout goComplain = inflate.findViewById(R.id.go_complain_auto);
        goComplain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deleteFriendReportOnclicke!=null)
                {
                    deleteFriendReportOnclicke.goComplain(index);
                }
            }
        });
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deleteFriendReportOnclicke!=null)
                    deleteFriendReportOnclicke.submit(index);
            }
        });
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (alertDialog!=null&&alertDialog.isShowing())
                alertDialog.cancel();
            }
        });

        alertDialog.show();
        alertDialog.getWindow().setContentView(inflate);
        Window window = alertDialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width= dp2px(context,284);
        attributes.height= dp2px(context,178);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(attributes);
        return alertDialog;
    }

    public AlertDialog showDeleteFriendReport(Context context,String title,String content, List<String> list, final DeleteFriendReportOnclicke deleteFriendReportOnclicke){
        index=-1;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog alertDialog = builder.create();
        View inflate = LayoutInflater.from(context).inflate(R.layout.delete_friend_report_dialog_layout, null);
        TextView contentView = inflate.findViewById(R.id.content);
        contentView.setText(content);
        TextView titleView = inflate.findViewById(R.id.title);
        titleView.setText(title);
        final ListView reportLV = inflate.findViewById(R.id.report_lv);
        //确定
        Button submit = inflate.findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deleteFriendReportOnclicke!=null)
                {
                    if (index<0)
                    {
                        ToastUtils.showShort("请选择要举报的原因");
                    }else{
                        deleteFriendReportOnclicke.submit(index);
                    }
                }
            }
        });
        final ReportListAdapter reportListAdapter = new ReportListAdapter(list, context);
        reportLV.setAdapter(reportListAdapter);
        reportListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                index=position;
                reportListAdapter.setPosition(position);
                reportListAdapter.notifyDataSetChanged();
            }
        });
        //去投诉
        AutoLinearLayout goComplain = inflate.findViewById(R.id.go_complain_auto);
        goComplain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deleteFriendReportOnclicke!=null)
                    deleteFriendReportOnclicke.goComplain(index);
            }
        });
        alertDialog.show();
        alertDialog.getWindow().setContentView(inflate);
        Window window = alertDialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width= dp2px(context,284);
        attributes.height= dp2px(context,445);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(attributes);
        return alertDialog;
    }

    /**
     * 用户选择自己管理的群弹窗
     * @param context
     * @param data
     * @return
     */
    public AlertDialog showReleaseForumAddGroup(Context context, final List<MyGroupListBean.DataBean> data, final OnSelectGroupListener onSelectGroupListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog alertDialog = builder.create();
        View inflate = LayoutInflater.from(context).inflate(R.layout.release_forum_add_group_dialog_layout, null);
        ListView listView = inflate.findViewById(R.id.list_view);
        ImageView quit = inflate.findViewById(R.id.quit_dialog);
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.e("ForumAddGroup", "onItemClick: "+i);
                if (onSelectGroupListener!=null)
                {
                    onSelectGroupListener.getSelectGroupID(data.get(i).getGroup_id(),i);
                }
                alertDialog.dismiss();
            }
        });
        ReleasePostAddGroupListAdapter releasePostAddGroupListAdapter = new ReleasePostAddGroupListAdapter(context,data);
        listView.setAdapter(releasePostAddGroupListAdapter);
        if (data.size()>=4)
        {
            View listItem  = listView.getAdapter().getView(1,null,listView);
            listItem.measure(0, 0);
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = listItem.getMeasuredHeight()*4+listView.getDividerHeight()*3;
            listView.setLayoutParams(params);
        }
        alertDialog.show();
        alertDialog.getWindow().setContentView(inflate);
        Window window = alertDialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams attributes = window.getAttributes();
        window.setBackgroundDrawable(null);
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(attributes);
        return alertDialog;
    }

    public interface ReportOnClickListener{
        void submit(int position);
    }
    public interface DeleteFriendReportOnclicke{
        void submit(int position);
        void goComplain(int position);
    }
    public interface OnSelectGroupListener{
        void getSelectGroupID(String groupId,int position);
    }
    public int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
